﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ZoneInZone.CApZZZoneInZoneRobotControllerUtilities
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Olp;


namespace DnProcessSimulateCommands.ZoneInZone;

internal class CApZZZoneInZoneRobotControllerUtilities
{
  public static ITxOlpRobotControllerParametersHandler GetRobotControllerParametersHandler(
    ITxOperation operation)
  {
    return new TxOlpControllerUtilities().InitAndGetRobotControllerParametersHandlerInstance(operation);
  }

  internal static bool IsRobotSetWithDefaultController(ITxRobot robot)
  {
    bool flag = false;
    if (robot.Controller.Name == "default")
      flag = true;
    else if ((ITxRoboticControllerServices) new TxOlpControllerUtilities().GetInterfaceImplementationFromController(robot.Controller.Name, typeof (ITxRoboticControllerServices), typeof (TxControllerAttribute), "ControllerName") == null)
      flag = true;
    return flag;
  }

  public static bool IsRobotSetWithVKRCController(ITxRobot robot)
  {
    return robot.Controller.Name == "Kuka-Vkrc";
  }

  public static List<string> GetZoneParameters(ITxRobot robot, ITxOperation robotOperation)
  {
    List<string> zoneParameters = new List<string>();
    if (robot != null)
    {
      if (CApZZZoneInZoneRobotControllerUtilities.IsRobotSetWithDefaultController(robot) || CApZZZoneInZoneRobotControllerUtilities.GetRobotControllerParametersHandler(robotOperation) == null)
        zoneParameters.Add("Zone");
      else if (CApZZZoneInZoneRobotControllerUtilities.IsRobotSetWithVKRCController(robot))
      {
        zoneParameters.Add("Ve");
      }
      else
      {
        ITxOlpRobotControllerParametersHandler parametersHandler = CApZZZoneInZoneRobotControllerUtilities.GetRobotControllerParametersHandler(robotOperation);
        string[] strArray = (string[]) null;
        if (parametersHandler != null)
          strArray = parametersHandler.GetFlybyParametersNames(robot as TxRobot);
        if (strArray != null)
        {
          for (int index = 0; index < strArray.Length; ++index)
          {
            if (strArray[index] != null)
              zoneParameters.Add(strArray[index]);
          }
        }
      }
    }
    return zoneParameters;
  }

  public static ArrayList GetLocationZoneParameters(
    ITxRoboticOperation loc,
    ITxRoboticOrderedCompoundOperation operation)
  {
    ArrayList locationZoneParameters = new ArrayList();
    if (operation.Robot != null && operation.Robot.Controller != null)
    {
      if (operation.Robot.Controller.Name == "default")
      {
        if (loc.GetParameter("RRS_ZONE_NAME") is TxRoboticStringParam parameter)
        {
          string str = parameter.Value;
          if (!string.IsNullOrEmpty(str))
            locationZoneParameters.Add((object) str);
        }
      }
      else
      {
        string name = operation.Robot.Controller.Name;
        List<string> zoneParameters = CApZZZoneInZoneRobotControllerUtilities.GetZoneParameters(operation.Robot, (ITxOperation) operation);
        TxPathEditorToDotNetGap editorToDotNetGap = new TxPathEditorToDotNetGap();
        string globalIdString = TxEngineeringDataInternal.GetGlobalIdString((ITxObject) loc);
        for (int index = 0; index < zoneParameters.Count; ++index)
        {
          string str1 = zoneParameters[index];
          string str2 = $"{name}_{str1}";
          string str3 = "";
          bool flag = false;
          editorToDotNetGap.HasComplexRepresentation(str2, globalIdString, ref flag);
          if (flag)
          {
            editorToDotNetGap.GetComplexRepresentation(ref str3, globalIdString, str2);
            if (str3 != null)
              str3 = str3.Trim();
          }
          locationZoneParameters.Add((object) str3);
        }
      }
    }
    return locationZoneParameters;
  }

  public static int GetZoneRadius(string zoneValue)
  {
    return CApZZZoneInZoneRobotControllerUtilities.GetAbbRapidZoneRadius(zoneValue);
  }

  public static int GetAbbRapidZoneRadius(string zoneValue)
  {
    int abbRapidZoneRadius = 0;
    Match match = Regex.Match(zoneValue, "([A-Za-z]+)([0-9]+)");
    if (match.Success)
      abbRapidZoneRadius = int.Parse(new string(match.ToString().Where<char>(new Func<char, bool>(char.IsDigit)).ToArray<char>()));
    return abbRapidZoneRadius;
  }

  public static ITxLocationOperation GetLocation(ITxOrderedObjectCollection operation, int pos)
  {
    ITxLocationOperation location = (ITxLocationOperation) null;
    if (pos >= 0 && pos < ((ITxObjectCollection) operation).Count && operation.GetChildAt(pos) is ITxRoboticLocationOperation childAt)
    {
      if (childAt is TxWeldLocationOperation)
      {
        if ((childAt as TxWeldLocationOperation).IsProjected)
          location = (ITxLocationOperation) childAt;
      }
      else
        location = (ITxLocationOperation) childAt;
    }
    return location;
  }

  public static List<ITxLocationOperation> GetOperationLocations(
    ITxOrderedObjectCollection operation)
  {
    List<ITxLocationOperation> operationLocations = new List<ITxLocationOperation>();
    for (int pos = 0; pos < ((ITxObjectCollection) operation).Count; ++pos)
    {
      ITxLocationOperation location = CApZZZoneInZoneRobotControllerUtilities.GetLocation(operation, pos);
      if (location != null)
        operationLocations.Add(location);
    }
    return operationLocations;
  }
}
